<?php

class AdminRoleService
{
    /*
     * 通过用户id获取用户的roleid
     *
     */
    public static function getRoleIDByUserID($user_id)
    {
        /*
        $searchstr = Util::initSearchSql()." and user_id=".$user_id;
        $obj = AdminUserRole::model()->find($searchstr);

        if(empty($obj))
            throw new Exception("该用户没有归属的角色");

        return $obj->role_id;
        */

        $adminuser = AdminUser::model()->findByPK($user_id);



        if(empty($adminuser))
            throw new Exception("用户不存在");


        $role_id = $adminuser->role_id;

        //Util::checkExistAndNotDelete($role_id);

        return $role_id;






    }

    /*
     * 通过role_id 返回可返回的所有菜单列表
     *
     */
    public static function getValidMenuList($role_id)
    {
        //初始化
        $searchsql = Util::initSearchSql();

        //获取所有菜单列表
        $menuidlist = AdminRoleMenu::model()->findAll($searchsql." and role_id = ".$role_id);

        //echo count($menuidlist);

        //返回list
        $returnlist = array();

        //获取所有一级菜单
        $firstlevellist = AdminMenu::model()->findAll($searchsql." and parent_id=-1");

        //print_r($menuidlist);

        //Util::responseObj($menuidlist);

        //ul
        $html = " <ul class=\"sidebar-menu\">";


        for($i=0;$i<count($firstlevellist);$i++)
        {
            $menu = $firstlevellist[$i];

            //此一级菜单存在
            if(AdminRoleService::isHasId($menu->menu_id,$menuidlist)==true)
            {
                array_push($returnlist,$firstlevellist[$i]);

                $html=$html." <li class=\"treeview\"  data-id=\"".$menu->menu_id."\"> <a href=\"#\"><i class=\"fa fa-indent\"></i>
                    <span>".$menu->menu_name."</span><i class=\"fa fa-angle-left pull-right\"></i></a>";



                //二级菜单
                //获取二级列表
                $smlist = AdminMenu::model()->findAll($searchsql." and parent_id=".$menu->menu_id);

                for($y=0;$y<count($smlist);$y++)
                {
                    $smenu = $smlist[$y];
                    //此二级菜单存在
                    if(AdminRoleService::isHasId($smenu->menu_id,$menuidlist)==true)
                        array_push($returnlist,$smenu);

                    $html= $html."  <ul class=\"treeview-menu\" data-id=\"".$smenu->menu_id."\">
                    <li class=\"".AdminRoleService::divideurl($smenu->url)."\" data-id=\"".$smenu->menu_id."\"><a href=\"".$smenu->url."\"><i class=\"fa fa-caret-right\"></i>".$smenu->menu_name."</a></li>
                </ul>";


                }


                $html=$html."</li>";


            }
            else
                continue;



        }

        $html=$html."</ul>";

        return $html;

       // return $returnlist;


    }

    public static  function TransfetToHtml($list)
    {

        //ul
        $html = " <ul class=\"sidebar-menu\">";

        $l = count($list);


        for($i=0;$i<$l;$i++)
        {
            $menu =$list[$i];

            if($menu->parent_id == -1)
            {
                $html=$html." <li class=\"treeview\"  data-id=\"".$menu->menu_id."\"> <a href=\"#\"><i class=\"fa fa-indent\"></i>
                    <span>".$menu->menu_name."</span><i class=\"fa fa-angle-left pull-right\"></i></a>";

                if($i+1<$l)
                {
                    $nmenu = $list[$i+1];

                    if($nmenu->parent_id == -1)
                        $html=$html."</li>";
                }

            }

            else
            {

                $html= $html."  <ul class=\"treeview-menu\" data-id=\"".$menu->menu_id."\">
                    <li class=\"".AdminRoleService::divideurl($menu->url)."\" data-id=\"".$menu->menu_id."\"><a href=\"".$menu->url."\"><i class=\"fa fa-caret-right\"></i>".$menu->menu_name."</a></li>
                </ul>";

            }

            if($i+1 >= $l)
                $html=$html."</li>";


        }


        $html=$html."</ul>";

        return $html;
    }

    //找出菜单列表中是否有mid
    public static function isHasId($mid,$list)
    {
        for($t=0;$t<count($list);$t++)
        {
            $mmid = $list[$t]->menu_id;


            if($mid == $mmid)
                return true;
        }



        return false;


    }



    public static function divideurl($url)
    {
        $strarr = explode("/",$url);

        return $strarr[count($strarr)-1];

    }


    /*
     * 新建角色名
     *
     */
    public  static  function addNewRole($rolename)
    {
        $role = AdminRole::model()->find("role_name='".$rolename."'");
        if(empty($role)==false)
        {
            if($role->is_delete == 0)
                throw new Exception("该角色名已存在");

        }



        //新增
        if(empty($role))
        {
            $role = new AdminRole();
            $role->role_name = $rolename;
            Util::initObjData($role);
        }
        else
        {
            //恢复
            Util::recoverObjData($role);

        }

        $r1= $role->save();

        if (!$r1) {

            // print_r($user->errors);

            throw new Exception("角色添加失败");
        }
    }

    /*
     * 删除角色
     *
     */
    public  static  function deleteRole($roleid)
    {
        $role = AdminRole::model()->findByPK($roleid);
        if(empty($role))
            throw new Exception("该角色不存在");

        //判断该角色下是否有用户
        $totalcount = AdminUser::model()->count("role_id=:role_id and is_delete = 0",
            array(":role_id" => $roleid));

        if($totalcount>0)
            throw new Exception("该角色下还有".$totalcount."个用户不能删除");


        //删除


        Util::deleteObjData($role);

        $r1= $role->save();

        if (!$r1) {

            // print_r($user->errors);

            throw new Exception("角色删除失败");
        }
    }

    /*
     * 更新角色
     *
     */
    public  static  function updateRole($roleid,$rolename)
    {
        $role = AdminRole::model()->findByPK($roleid);
        Util::checkExistAndNotDelete($role);



        //更新
        $role->role_name = $rolename;
        Util::updateObjData($role);

        $r1= $role->save();

        if (!$r1) {

            // print_r($user->errors);

            throw new Exception("角色更新失败");
        }
    }



}


?>